package com.car.loan.framework.aspectj.impl;

import com.car.loan.common.enums.LoanStatusEnum;
import com.car.loan.common.utils.StringUtils;
import com.car.loan.framework.aspectj.DataStatusSqlService;
import com.car.loan.project.xr.util.bo.business.LoanBaseInfo;
import com.car.loan.project.xr.util.enums.BusinessApostilleStatus;
import com.car.loan.project.xr.util.enums.FileAuditStatus;
import com.car.loan.project.xr.util.enums.LoanBusinessAuditStatus;
import com.car.loan.project.xr.util.enums.LoanBusinessStatus;
import java.util.Optional;
import org.springframework.stereotype.Service;

/**
 * @author huangding
 * @description
 * @date 2019/11/14 11:11
 */
@Service
public class BusinessAuditStatusSqlServiceImpl implements DataStatusSqlService {

    @Override
    public LoanStatusEnum getLoanStatusEnum() {
        return LoanStatusEnum.BUSINESS_AUDIT_STATUS;
    }

    @Override
    public LoanBaseInfo getStatusRemark(LoanBaseInfo loanBaseInfo) {
        Long businessAuditStatus = loanBaseInfo.getBusinessAuditStatus();
        Long businessApostilleStatus = loanBaseInfo.getBusinessApostilleStatus();
        String statusRemark = StringUtils.EMPTY;
        if (LoanBusinessAuditStatus.INIT.status == businessAuditStatus) {
            statusRemark = "待审批";
        } else if (businessAuditStatus == LoanBusinessAuditStatus.APOSTILLE.status &&
            businessApostilleStatus == BusinessApostilleStatus.INIT.status) {
            statusRemark = "待加签";
        } else if (businessAuditStatus == LoanBusinessAuditStatus.PASS.status ||
            businessApostilleStatus == BusinessApostilleStatus.PASS.status) {
            statusRemark = "已审批";
        } else if (businessAuditStatus == LoanBusinessAuditStatus.GO_BACK.status ||
            businessApostilleStatus == BusinessApostilleStatus.GO_BACK.status) {
            statusRemark = "已退回";
        } else if (businessAuditStatus == LoanBusinessAuditStatus.REJECT.status ||
            businessApostilleStatus == BusinessApostilleStatus.REJECT.status) {
            statusRemark = "已拒绝";
        }
        loanBaseInfo.setStatusRemark(statusRemark);
        return loanBaseInfo;
    }

    @Override
    public String getSortSql(String statusAlias) {
        return " order by {}.business_time desc".replace("{}", statusAlias);
    }

    @Override
    public String getSql(String statusAlias, Integer status) {

        String sql =
            "{}.file_audit_status = " + FileAuditStatus.INIT.status + " and {}.business_status = "
                + LoanBusinessStatus.SUBMIT.status;
        if (status != null) {
            switch (BusinessAuditStatus.getBusinessAuditStatus(status)
                .orElse(BusinessAuditStatus.INIT)) {
                case INIT:
//                    0待审批(业务申请已提交且业务审批未提交，默认）
                    sql +=
                        " and {}.business_audit_status =  " + LoanBusinessAuditStatus.INIT.status;
                    break;
                case INIT_APOSTILLE:
//                    1待加签（业务审批加签且加签审批未提交）
                    sql += " and {}.business_audit_status =  "
                        + LoanBusinessAuditStatus.APOSTILLE.status
                        + " and {}.business_apostille_status = "
                        + BusinessApostilleStatus.INIT.status;
                    break;
                case PASS:
//                    2已审批（业务审批同意或业务审批加签且加签审批同意）
                    sql +=
                        " and ({}.business_audit_status =  " + LoanBusinessAuditStatus.PASS.status
                            + " or {}.business_apostille_status= "
                            + BusinessApostilleStatus.PASS.status
                            + " ) ";
                    break;
                case GO_BACK:
//                    3已退回（业务审批退回或加签审批退回）
                    sql += " and ({}.business_audit_status =  "
                        + LoanBusinessAuditStatus.GO_BACK.status
                        + "  or {}.business_apostille_status = "
                        + BusinessApostilleStatus.GO_BACK.status + " ) ";

                    break;
                case REJECT:
//                    4已拒绝（业务审批拒绝或加签审批拒
                    sql += " and ({}.business_audit_status =  "
                        + LoanBusinessAuditStatus.REJECT.status
                        + "  or {}.business_apostille_status = "
                        + BusinessApostilleStatus.REJECT.status + " )";
                    break;
                default:

            }
        }
        return sql.replace("{}", statusAlias);
    }

    public static void main(String[] args) {
        System.out.println(new BusinessAuditStatusSqlServiceImpl().getSql("s", 0));
        System.out.println(new BusinessAuditStatusSqlServiceImpl().getSql("s", 1));
        System.out.println(new BusinessAuditStatusSqlServiceImpl().getSql("s", 2));
        System.out.println(new BusinessAuditStatusSqlServiceImpl().getSql("s", 3));
        System.out.println(new BusinessAuditStatusSqlServiceImpl().getSql("s", 4));

    }

    enum BusinessAuditStatus {
        /**
         *
         */
        INIT(0),
        INIT_APOSTILLE(1),
        PASS(2),
        GO_BACK(3),
        REJECT(4);

        BusinessAuditStatus(int status) {
            this.status = status;
        }

        private static Optional<BusinessAuditStatus> getBusinessAuditStatus(int status) {
            for (BusinessAuditStatus businessAuditStatus : values()) {
                if (businessAuditStatus.status == status) {
                    return Optional.of(businessAuditStatus);
                }
            }
            return Optional.empty();
        }


        public final int status;

    }
}
